﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    [Table("T_Download")]
    public class T_Download : IEntity<T_Download>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(256)]
        public string Title { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(512)]
        public string Remark { get; set; }

        [Column(TypeName="datetime")]
        public DateTime CreateDate { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(2000)]
        public string Path { get; set; }
        /// <summary>
        /// 0:站点下载文件 1：期刊下载文件 2：会议下载文件
        /// </summary>
        public int Type { get; set; }

        public int OrgType { get; set; }
        public int OrgId { get; set; }

        public override T_Download Find(string key)
        {
            int id = 0;
            Int32.TryParse(key,out id);
            return Find(c => c.Id == id);
        }

        public override T_Download Find(System.Linq.Expressions.Expression<Func<T_Download, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Download.FirstOrDefault(where);
                if (item != null)
                {
                    this.CreateDate = item.CreateDate;
                    this.Id = item.Id;
                    this.Path = item.Path;
                    this.Remark = item.Remark;
                    this.Title = item.Title;
                    this.Type = item.Type;
                    this.OrgId = item.OrgId;
                    this.OrgType = item.OrgType;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Download, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Download.Any(where);
            }
        }
    }
}
